package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcRepoSupplier;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.RepoSupplierForm;
import com.yxinsur.imc.business.mapper.ImcRepoSupplierMapper;
import com.yxinsur.imc.business.service.IImcRepoSupplierService;
import com.yxinsur.imc.business.vo.RepoSupplierVo;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商库Service业务层处理
 * 
 * @author imc
 * @date 2021-03-25
 */
@Service
public class ImcRepoSupplierServiceImpl extends ServiceImpl<ImcRepoSupplierMapper, ImcRepoSupplier> implements IImcRepoSupplierService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcRepoSupplierServiceImpl.class);

    @Autowired
    private ImcRepoSupplierMapper imcRepoSupplierMapper;
    /**
     * 查询供应商库列表
     * 
     * @param supplierForm 供应商库
     * @return 供应商库
     */
    @Override
    public List<RepoSupplierVo> selectImcRepoSupplierList(RepoSupplierForm supplierForm)
    {
        logger.info("供应商/保险公司库列表查询，条件：{}",supplierForm.toString());
        return imcRepoSupplierMapper.selectRepoSupplierList(supplierForm);
    }

    /**
     * 新增供应商库
     * 
     * @param supplierForm 供应商库
     * @return 结果
     */
    @Override
    public int insertImcRepoSupplier(RepoSupplierForm supplierForm)
    {
        logger.info("供应商/保险公司库新增操作：{}",supplierForm.toString());
        ImcRepoSupplier repoSupplier = new ImcRepoSupplier();
        BeanUtils.copyProperties(supplierForm,repoSupplier);
        repoSupplier.setCreateTime(DateUtils.getNowDate());
        repoSupplier.setCreateId(SecurityUtils.getUsername());
        repoSupplier.setIsDeleted(IsDeletedEnum.N.getCode());
        repoSupplier.setTenantId(SecurityUtils.getTenantId());
        return imcRepoSupplierMapper.insert(repoSupplier);
    }

    /**
     * 修改供应商库
     * 
     * @param supplierForm 供应商库
     * @return 结果
     */
    @Override
    public int updateImcRepoSupplier(RepoSupplierForm supplierForm)
    {
        logger.info("供应商/保险公司库更新操作：{}",supplierForm.toString());
        ImcRepoSupplier repoSupplier = this.imcRepoSupplierMapper.selectById(supplierForm.getId());
        BeanUtils.copyProperties(supplierForm,repoSupplier);
        repoSupplier.setUpdateTime(DateUtils.getNowDate());
        repoSupplier.setUpdateId(SecurityUtils.getUsername());
        return imcRepoSupplierMapper.updateById(repoSupplier);
    }

    /**
     * 删除供应商库
     *
     * @param ids 供应商库id
     * @return 结果
     */
    @Override
    public int deleteImcRepoSupplier(Long[] ids) {
        logger.info("供应商/保险公司库删除操作：{}",ids);
        int count = 0;
        for(Long id:ids){
            ImcRepoSupplier repoSupplier = this.imcRepoSupplierMapper.selectById(id);
            repoSupplier.setIsDeleted(repoSupplier.getId().toString());
            repoSupplier.setUpdateId(SecurityUtils.getUsername());
            repoSupplier.setUpdateTime(DateUtils.getNowDate());
            count += this.imcRepoSupplierMapper.updateById(repoSupplier);
        }
        return count;
    }
}
